define(
	[
		'require',
		'broada/base/text!./template.hbs'
	],
	function (require) {
		"use strict";
		var tpl = require('broada/base/text!./template.hbs');

		var Component = Ember.Component.extend({

			layout: Ember.HTMLBars.compile(tpl),

			didInsertElement: function(){
				this.setDefaultDate();
				this.getIpsAndProtosData(this.get('appId'));
			},

			setDefaultDate: function(){
				this.updateBtnTxt();
			},

			updateBtnTxt: function(){
				var ipItem= this.get("ipChooseItem");
				var portoalItem = this.get("protoChooseItem");
				var showItem = ipItem?(portoalItem?ipItem + ":" +portoalItem:ipItem):(portoalItem?portoalItem:"请选择业务组件");
				this.set("tradesAllbtnTxt", showItem);
			},

			getIpsAndProtosData: function(appId) {
				var view = this;
				var store = this.container.lookup("store:apm");
				store.ajax("GET", "applications/"+appId+"/ips")
				.then(function(response){
					view.set("ips",Ember.A(response.ips));
				}).then(function(){
					store.ajax("GET", "applications/"+appId+"/protos")
					.then(function(response){
						view.set("protos",Ember.A(response.protos));
					});
				});
			},

			selectedbtnTxtChange: Ember.observer('tradesAllbtnTxt', function(){
				var tradesAllbtnTxt = this.get("tradesAllbtnTxt");
			}),

			selectedIpChange: Ember.observer('ipChooseItem', function(){
				var ipChooseItem = this.get("ipChooseItem");
				var $selector = this.$().find('.ip-select');
				var $lis = $selector.find("li").removeClass("active");

				if(ipChooseItem != null){
					$lis.each(function(){
						var length = this.innerHTML.length-41;
						if(this.innerHTML.indexOf(ipChooseItem) != -1 && length == ipChooseItem.length){
							$(this).addClass("active");
							return;
						}
					});
				}
			}),

			selectedPlChange: Ember.observer('protoChooseItem', function(){
				var protoChooseItem = this.get("protoChooseItem");
				var $selector = this.$().find('.proto-select');
				var $lis = $selector.find("li").removeClass("active");

				if(protoChooseItem != null){
					$lis.each(function(){
						var length = this.innerHTML.length-41;
						if(this.innerHTML.indexOf(protoChooseItem) != -1 && length == protoChooseItem.length){
							$(this).addClass("active");
							return;
						}
					});
				}
			}),

			actions: {
				showTradesAllSelect:function(){
					this.$().find('.tradesAll-d').show();
				},

				hideTradesAllSelect:function(){
					this.$().find('.tradesAll-d').hide();
				},

				tradesAllUiliIp:function(ip){
					this.set("ipChooseItem",ip);
				},

				tradesAllUiliPl:function(proto){
					this.set("protoChooseItem",proto);
				},

				chooseItem:function(){
					this.updateBtnTxt();
					this.send("hideTradesAllSelect");
				},

				clearItem:function(){
					this.set("ipChooseItem","");
					this.set("protoChooseItem","");
					this.updateBtnTxt();
					this.send("hideTradesAllSelect");
				},

				mouseLeaveTradesAllSelect:function(){
					this.send("hideTradesAllSelect");
				}
			}
		});

		return Component;
	}
);